/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: promise.h
 *
 * Purpose: ara::core specific variant of std::promise class
 *
 * Developer:
 *   wen.gu , 2020-04-14
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_PROMISE_H__
#define __ARA_CORE_PROMISE_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <mutex>
#include <future>
#include <type_traits>

#include "ara/core/error_code.h"
#include "ara/core/future.h"
#include "ara/core/internal/future_internal.h"


/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/


template <typename T, typename E = ErrorCode>
class Promise
{
public:
    using auto_lock = std::unique_lock<std::mutex>;
    using value_type = Result<T, E>;
    using value_ptr = std::shared_ptr<value_type>;
    using promise_type = std::promise<value_ptr>;
    using internal_state_type = internal_impl::FutureInternalState;
    using state_ptr = std::shared_ptr<internal_state_type>;

public:
    Promise()
        :mInnerState(std::make_shared<internal_state_type>())
    {
        /** todo something */
    }

    Promise(Promise&& other) noexcept
        :mPromise(std::move(other.mPromise)),
        mInnerState(std::move(other.mInnerState))
    {
        /** todo something */ 
    }

    ~Promise()
    {
        /** todo something */
    }

public:
    Promise& operator= (Promise&& other)
    {
        mPromise = std::move(other.mPromise);
        mInnerState = std::move(other.mInnerState);
    }

    void swap(Promise& other) noexcept
    {
        mPromise.swap(other.mPromise);
        mInnerState.swap(other.mInnerState);
    }

public:
    Future<T, E> get_future()
    {
        return std::move(Future<T, E>(std::move(mPromise.get_future()), mInnerState));
    }

    void set_value(T const& value)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<T, E> >(value));
        mInnerState->DoCommunication();
    }

    void set_value(T&& value)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<T, E> >(std::move(value)));
        mInnerState->DoCommunication();
    }

    void SetError(E&& error)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<T, E> >(std::move(error)));
        mInnerState->DoCommunication();
    }

    void SetError(E const& error)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<T, E> >(error));
        mInnerState->DoCommunication();
    }

private:
    Promise(Promise const&) = delete;
    Promise& operator= (Promise const&) = delete;

private:
    promise_type mPromise;
    state_ptr mInnerState;
    std::mutex mLock;
};


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/** his section defines the interface of the ara::core::Promise<T,E> template 
 *  specialization where the type T is void
 */
template <typename E>
class Promise< void, E> final
{
public:
    using auto_lock = std::unique_lock<std::mutex>;
    using value_type = Result<void, E>;
    using value_ptr = std::shared_ptr<value_type>;
    using promise_type = std::promise<value_ptr>;
    using internal_state_type = internal_impl::FutureInternalState;
    using state_ptr = std::shared_ptr<internal_state_type>;
public:
    Promise()
        :mInnerState(std::make_shared<internal_state_type>())
    {
        /** todo something */
    }

    Promise(Promise&& other) noexcept
        :mPromise(std::move(other.mPromise)),
        mInnerState(std::move(other.mInnerState))
    {
        /** todo something */
    }

public:
    Promise& operator= (Promise&& other)
    {
        mPromise = std::move(other.mPromise);
        mInnerState = std::move(other.mInnerState);
    }

    void swap(Promise& other) noexcept
    {
        mPromise.swap(other.mPromise);
        mInnerState.swap(other.mInnerState);
    }

public:
    Future<void, E> get_future()
    {
        return std::move(Future<void, E>(std::move(mPromise.get_future()), mInnerState));
    }

    void set_value()
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<void, E> >(value_type::FromValue()));
        mInnerState->DoCommunication();
    }

    void SetError(E&& error)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<void, E> >(std::move(error)));
        mInnerState->DoCommunication();
    }

    void SetError(E const& error)
    {
        auto_lock al(mLock);
        mPromise.set_value(std::make_shared<Result<void, E> >(error));
        mInnerState->DoCommunication();
    }
private:
    Promise(Promise const&) = delete;
    Promise& operator= (Promise const&) = delete;

private:
    promise_type mPromise;
    state_ptr mInnerState;
    std::mutex mLock;
};



} /** namespace core */
} /** namespace ara */

#endif /** !__ARA_CORE_PROMISE_H__ */

